{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "936ad3be",
   "metadata": {},
   "source": [
    "# Day 1：环境与 Python 基础认知 (低门槛版)\n",
    "\n",
    "周贤中\n",
    "\n",
    "zhouxzh@gdut.edu.cn\n",
    "\n",
    "> 目标：会进入 Jupyter，能写最基本 Python 语句，理解 *数据分析最小流程*：提出问题 → 获取数据 → 处理清洗 → 探索分析 → 可视化表达 → 得出结论。\n",
    "\n",
    "本 Notebook 结构：\n",
    "1. 准备与环境说明\n",
    "2. Python 基础语法最小集合\n",
    "3. 列表(list) & 字典(dict) 组织数据\n",
    "4. 用代码表示一张“同学成绩小表”\n",
    "5. 迷你案例：计算 5 名同学平均分\n",
    "6. 小结\n",
    "7. 课后作业提示\n",
    "\n",
    "> 建议边看边在右侧/下方新建空单元格自己敲一遍。敲错是学习的一部分。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e10abdd",
   "metadata": {},
   "source": [
    "## 1. 环境快速说明\n",
    "- 我们使用 **Anaconda** 来一键管理 Python 及常用科学计算库。\n",
    "- 使用 **Jupyter Notebook** 作为交互式实验本：一格格单元 = 思路 + 代码 + 输出。\n",
    "- 保存文件：Ctrl+S；运行单元：Shift+Enter。\n",
    "- 新建单元：上方工具栏按钮或使用快捷键 (在命令模式下 A=上方插入, B=下方插入)。\n",
    "\n",
    "### 什么时候需要重启内核 (Kernel)?\n",
    "- 变量乱了/命名冲突/长时间没清理。\n",
    "- 重启后变量会全部消失, 需要重新运行前面单元。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e6a3ab1",
   "metadata": {},
   "source": [
    "## 2. Python 基础语法最小集合\n",
    "下面这些足以支撑前两天学习。后面逐步加。\n",
    "\n",
    "| 功能 | 例子 | 说明 |\n",
    "|------|------|------|\n",
    "| 输出 | print('Hello') | 查看结果 |\n",
    "| 变量 | x = 10 | 变量=数据 |\n",
    "| 基本类型 | int 10 / float 3.14 / str 'hi' | 数字/小数/字符串 |\n",
    "| 容器 | list [1,2,3] / dict {'a':1} | 组织多项数据 |\n",
    "| 计算 | + - * / // % | 四则 / 整除 / 取余 |\n",
    "| 比较 | > < >= <= == != | 得到 True/False |\n",
    "| 逻辑 | and / or / not | 连接判断 |\n",
    "| 条件 | if 条件: ... else: ... | 分支执行 |\n",
    "| 循环 | for item in list: ... | 重复操作 |\n",
    "| 导入 | import math | 使用库功能 |\n",
    "\n",
    "> 记忆技巧：先理解“为什么这样写”，再记语法。多写自然会熟。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62bc0e46",
   "metadata": {},
   "source": [
    "### 更多基础语法要点\n",
    "- 字符串格式化：f\"姓名:{name} 分数:{score}\" \n",
    "- 列表方法：append / pop / len\n",
    "- 字典方法：keys() / values() / get()（安全取值）\n",
    "- 内置函数：len / sum / max / min / round\n",
    "- 运算优先级：括号 > 乘除 > 加减\n",
    "下面追加代码示例。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6017e6bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Data Analysis 2025\n",
      "加法 a + b = 17\n",
      "减法 a - b = 7\n",
      "乘法 a * b = 60\n",
      "除法 a / b = 2.4\n",
      "整除 a // b = 2\n",
      "取余 a % b = 2\n",
      "幂运算 a ** b = 248832\n"
     ]
    }
   ],
   "source": [
    "# 试运行：输出与变量\n",
    "message = 'Hello Data Analysis'\n",
    "year = 2025\n",
    "print(message, year)\n",
    "\n",
    "# 基本计算\n",
    "a = 12\n",
    "b = 5\n",
    "print('加法 a + b =', a + b)\n",
    "print('减法 a - b =', a - b)\n",
    "print('乘法 a * b =', a * b)\n",
    "print('除法 a / b =', a / b)\n",
    "print('整除 a // b =', a // b)\n",
    "print('取余 a % b =', a % b)\n",
    "print('幂运算 a ** b =', a ** b) # C语言幂运算符号为 ^"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9255686",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm \"stupid\" \n"
     ]
    }
   ],
   "source": [
    "message = \"I'm \\\"stupid\\\" \" # 转义字符\n",
    "print(message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "27457923",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:张三 语文:78 数学:85 英语:90 平均:84\n"
     ]
    }
   ],
   "source": [
    "# 更多语法示例\n",
    "name = '张三'\n",
    "score_chi, score_math, score_eng = 78, 85, 90\n",
    "# f-string 格式化输出 format-string\n",
    "print(f'姓名:{name} 语文:{score_chi} 数学:{score_math} 英语:{score_eng} 平均:{(score_chi+score_math+score_eng)/3:.0f}')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "64d2ba53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "修改后的列表: [1, 5, 10] 弹出的元素: 3\n",
      "列表长度 len(nums)= 3\n",
      "最大值/最小值: 10 1\n"
     ]
    }
   ],
   "source": [
    "# 列表常用方法\n",
    "nums = [3, 1, 5]\n",
    "nums.append(10)   # 末尾添加 nums = [3, 1, 5, 10]\n",
    "first = nums.pop(0) # 弹出索引 0 位置元素 nums = [1, 5, 10], first = 3\n",
    "print('修改后的列表:', nums, '弹出的元素:', first)\n",
    "print('列表长度 len(nums)=', len(nums))\n",
    "print('最大值/最小值:', max(nums), min(nums))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "a8836bd1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 1, 5]\n",
      "[10, 5, 5]\n"
     ]
    }
   ],
   "source": [
    "nums = [3, 1, 5]\n",
    "\n",
    "nums[0] = 10 # 修改索引 0 位置元素\n",
    "print(nums)\n",
    "\n",
    "nums[1] = 5 # 修改索引 1 位置元素\n",
    "print(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eba5dca9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 4]\n",
      "[0, 1, 2]\n",
      "[5, 6, 7, 8, 9]\n",
      "[7, 8, 9]\n",
      "[0, 1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    "\n",
    "print(nums[2:5]) # 切片操作(for i = 2; i < 5; i++)\n",
    "\n",
    "print(nums[:3])   # 从头到索引 3 位置（不包括 3）\n",
    "print(nums[5:])   # 从索引 5 位置到尾部\n",
    "print(nums[-3:])  # 从倒数第 3 个到尾部\n",
    "print(nums[:-3])  # 从头到倒数第 3 个（不包括倒数第 3 个）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ca5983d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 1, 2, 3, 4, 5)\n"
     ]
    }
   ],
   "source": [
    "nums = (0, 1, 2, 3, 4, 5) # 元组定义\n",
    "print(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3bff7e40",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表: [10, 1, 2, 3, 4, 5]\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[21], line 5\u001b[0m\n\u001b[0;32m      3\u001b[0m nums_list[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m10\u001b[39m  \u001b[38;5;66;03m# 列表可变\u001b[39;00m\n\u001b[0;32m      4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m列表:\u001b[39m\u001b[38;5;124m'\u001b[39m, nums_list)\n\u001b[1;32m----> 5\u001b[0m nums_tuple[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m10\u001b[39m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "nums_list = [0, 1, 2, 3, 4, 5] # 列表定义\n",
    "nums_tuple = (0, 1, 2, 3, 4, 5) # 元组定义\n",
    "nums_list[0] = 10  # 列表可变\n",
    "print('列表:', nums_list)\n",
    "nums_tuple[0] = 10 # 元组不可变，尝试修改会报错\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "53b0bb7e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三 的成绩是 78\n",
      "李四 的成绩是 85\n",
      "王五 的成绩是 90\n"
     ]
    }
   ],
   "source": [
    "name = ['张三', '李四', '王五']\n",
    "score = [78, 85, 90]\n",
    "print(name[0], '的成绩是', score[0]) # 张三 的成绩是 78\n",
    "print(name[1], '的成绩是', score[1]) # 李四 的成绩是 85\n",
    "print(name[2], '的成绩是', score[2]) # 王五 的成绩是 90"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "991c7c6b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三的成绩: 85\n",
      "李四的成绩: 90\n",
      "修改后王五的成绩: 80\n"
     ]
    }
   ],
   "source": [
    "# 字典常用方法\n",
    "# {key: value, key2: value2} \n",
    "# {健: 值, 健2: 值2}\n",
    "student = {'张三': 85, '李四': 90, '王五': 78} \n",
    "print('张三的成绩:', student['张三']) # 通过键获取值\n",
    "print('李四的成绩:', student['李四']) # 通过键获取值\n",
    "student['王五'] = 80 # 修改键对应的值\n",
    "print('修改后王五的成绩:', student['王五'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9f7f163f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三\n",
      "无成绩键\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 字典 get 安全取值\n",
    "info = {'name': '张三', 'age': 18}\n",
    "print(info.get('name'))       # 正常存在\n",
    "print(info.get('score', '无成绩键'))  # 不存在时返回默认\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "399d5b1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "及格\n",
      "1~5 累加 = 15\n"
     ]
    }
   ],
   "source": [
    "# 条件与循环示例\n",
    "score = 85\n",
    "if score >= 60:\n",
    "    print('及格')\n",
    "else:\n",
    "    print('不及格')\n",
    "\n",
    "# for 循环：累加 1~5\n",
    "total = 0\n",
    "for i in [1, 2, 3, 4, 5]:\n",
    "    total = total + i  # 也可以写 total += i\n",
    "print('1~5 累加 =', total)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d47c9df0",
   "metadata": {},
   "source": [
    "## 3. 列表(list) 与 字典(dict)\n",
    "为什么需要？因为要把多条数据放在一起。\n",
    "\n",
    "- list 有顺序，用下标访问：students[0]。\n",
    "- dict 通过“键”访问：scores['语文']。\n",
    "\n",
    "记忆：list = 有序排队；dict = 带标签的抽屉。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4ebeeb39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学生列表: ['张三', '李四', '王五', '赵六', '周七']\n",
      "第一个学生: 张三\n",
      "张三数学成绩: 85\n"
     ]
    }
   ],
   "source": [
    "# 列表示例：保存 5 个学生姓名\n",
    "students = ['张三', '李四', '王五', '赵六', '周七']\n",
    "print('学生列表:', students)\n",
    "print('第一个学生:', students[0])\n",
    "\n",
    "# 字典示例：单个学生成绩\n",
    "score_zhang = {\n",
    "    'name': '张三',\n",
    "    '语文': 78,\n",
    "    '数学': 85,\n",
    "    '英语': 90\n",
    "}\n",
    "print('张三数学成绩:', score_zhang['数学'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e83fc84",
   "metadata": {},
   "source": [
    "## 程序控制"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56a47ec3",
   "metadata": {},
   "source": [
    "### 选择语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "e94e3cbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 单分支\n",
    "# score = 85\n",
    "score = 60\n",
    "\n",
    "if score > 60:\n",
    "    print('及格')\n",
    "\n",
    "if score < 60:\n",
    "    print('不及格')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "5d203844",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "及格\n"
     ]
    }
   ],
   "source": [
    "# 双分支\n",
    "score = 85\n",
    "# score = 59\n",
    "if score >= 60:\n",
    "    print('及格')\n",
    "else:\n",
    "    print('不及格')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "fabb46fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "良好\n"
     ]
    }
   ],
   "source": [
    "score = 85\n",
    "### 选择语句  \n",
    "if score >= 90:\n",
    "    print('优秀')\n",
    "elif score >= 80:\n",
    "    print('良好')\n",
    "elif score >= 70:\n",
    "    print('一般')\n",
    "elif score >= 60:\n",
    "    print('及格')\n",
    "else:\n",
    "    print('不及格')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afe3deca",
   "metadata": {},
   "source": [
    "## 循环控制语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "f1f1fcdf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4]\n",
      "[5, 6, 7, 8, 9]\n",
      "[0, 2, 4, 6, 8]\n"
     ]
    }
   ],
   "source": [
    "# range 三种示例\n",
    "a = range(5)\n",
    "b = range(5, 10)   \n",
    "c = range(0, 10, 2) # 0~10 步长为 2\n",
    "print(list(a))\n",
    "print(list(b))\n",
    "print(list(c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "ebb1fa27",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# 循环控制语句\n",
    "\n",
    "for i in range(5): # C语言 for (i = 0; i < 5; i++)\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "ace48018",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 6): # C语言 for (i = 1; i < 6; i++)\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "5a7dab9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 6, 2): # C语言 for (i = 1; i < 6 ; i+=2)\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "81fa52b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "a = [0, 1, 2, 3, 4]\n",
    "b = [5, 6, 7, 8, 9]\n",
    "c = a + b\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "cbaef745",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "a = list(range(5))\n",
    "b = list(range(5, 10))\n",
    "c = a + b\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "db7d6d40",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "# 用for循环实现100以内的数字的和\n",
    "total = 0\n",
    "for i in range(1, 101):\n",
    "    total += i\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "59556d81",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "4\n",
      "3\n",
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# while 循环示例\n",
    "n = 5   # 控制循环次数  \n",
    "while n > 0:\n",
    "    print(n)\n",
    "    n -= 1  # n = n - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "58dff21c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "4\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# break 示例\n",
    "n = 5  \n",
    "while n > 0:\n",
    "    print(n)\n",
    "    if n == 3:\n",
    "        break\n",
    "    n -= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "03802b73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "4\n",
      "3\n",
      "2\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "n = 5  \n",
    "while True:\n",
    "    print(n)\n",
    "    if n == 0:\n",
    "        break\n",
    "    n -= 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "01ab8cb2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "2\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# continue 示例\n",
    "n = 5   \n",
    "while n > 0:\n",
    "    n -= 1\n",
    "    if n == 3:\n",
    "        continue\n",
    "    print(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "c92e4880",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "# 计算100以内的奇数和\n",
    "total = 0\n",
    "for i in range(1, 101):\n",
    "    if i % 2 == 0:\n",
    "        continue\n",
    "    total += i\n",
    "\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "1b2897c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "# 计算100以内的奇数和\n",
    "total = 0\n",
    "for i in range(1, 101):\n",
    "    if i % 2 == 1:\n",
    "        total += i\n",
    "\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "935f6261",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "total = 0\n",
    "i = 1\n",
    "while i <= 100:\n",
    "    if i % 2 == 1:\n",
    "        total += i\n",
    "    i += 1\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a17be17",
   "metadata": {},
   "source": [
    "## 4. 用 list + dict 组织“同学成绩小表”\n",
    "思路：一张表 = 多行；每行 = 一个字典；多行放到一个列表。\n",
    "\n",
    "字段（键）统一：name / 语文 / 数学 / 英语。\n",
    "\n",
    "后续用 pandas DataFrame 就是对这种结构的强化版本。现在先理解基本思想。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "8bd2d285",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "前两条记录:\n",
      "{'姓名': '张三', '语文': 78, '数学': 85, '英语': 90}\n",
      "{'姓名': '李四', '语文': 82, '数学': 88, '英语': 76}\n",
      "第三个学生英语成绩: 89\n"
     ]
    }
   ],
   "source": [
    "# 创建成绩'表'：列表中包含多个学生记录（字典）\n",
    "grade_table = [\n",
    "    {'姓名': '张三', '语文': 78, '数学': 85, '英语': 90},\n",
    "    {'姓名': '李四', '语文': 82, '数学': 88, '英语': 76},\n",
    "    {'姓名': '王五', '语文': 90, '数学': 92, '英语': 89},\n",
    "    {'姓名': '赵六', '语文': 67, '数学': 74, '英语': 70},\n",
    "    {'姓名': '周七', '语文': 88, '数学': 79, '英语': 84},\n",
    "]\n",
    "\n",
    "# 查看前两个\n",
    "print('前两条记录:')\n",
    "for row in grade_table[:2]:\n",
    "    print(row)\n",
    "\n",
    "# 访问第三个学生的英语成绩\n",
    "third_english = grade_table[2]['英语']\n",
    "print('第三个学生英语成绩:', third_english)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9cf551d",
   "metadata": {},
   "source": [
    "## 5. Mini Case：统计 5 名同学平均分\n",
    "需求：计算每个学生的三科平均分，再计算全班总平均。\n",
    "\n",
    "拆解：\n",
    "1. 遍历每个字典 row\n",
    "2. 读取语文/数学/英语 3 个值求和除 3\n",
    "3. 保存到一个新列表或直接打印\n",
    "4. 累加所有平均分再除以学生人数\n",
    "\n",
    "提示：尽量把 \"固定写法\" 记下来，未来会在 pandas 中更简单。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "70393918",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三的平均分: 84.3\n",
      "李四的平均分: 82.0\n",
      "王五的平均分: 90.3\n",
      "赵六的平均分: 70.3\n",
      "周七的平均分: 83.7\n",
      "--------------------\n",
      "全班平均分: 82.13\n"
     ]
    }
   ],
   "source": [
    "# 计算每个学生个人平均分 + 全班平均分\n",
    "student_avgs = []\n",
    "for row in grade_table:\n",
    "    per_avg = (row['语文'] + row['数学'] + row['英语']) / 3\n",
    "    student_avgs.append(per_avg)\n",
    "    print(f'{row[\"姓名\"]}的平均分: {per_avg:.1f}')\n",
    "\n",
    "class_avg = sum(student_avgs) / len(student_avgs)\n",
    "print('-' * 20)\n",
    "print('全班平均分:', round(class_avg, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f16f2c54",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "全班语文平均分: 81.0\n"
     ]
    }
   ],
   "source": [
    "sum_chinese = 0\n",
    "sum_math = 0\n",
    "sum_english = 0\n",
    "for row in grade_table:\n",
    "    sum_chinese += row['语文']\n",
    "    sum_math += row['数学']\n",
    "    sum_english += row['英语']\n",
    "avg_chinese = sum_chinese / len(grade_table)\n",
    "print('全班语文平均分:', round(avg_chinese, 2))\n",
    "avg_math = sum_math / len(grade_table)\n",
    "print('全班数学平均分:', round(avg_math, 2))\n",
    "avg_english = sum_english / len(grade_table)\n",
    "print('全班英语平均分:', round(avg_english, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d0d07a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "全班语文平均分: 81.0\n",
      "全班数学平均分: 83.6\n",
      "全班英语平均分: 81.8\n"
     ]
    }
   ],
   "source": [
    "avg_chinese = sum([row['语文'] for row in grade_table]) / len(grade_table) # 列表推导式\n",
    "print('全班语文平均分:', round(avg_chinese, 2))\n",
    "\n",
    "avg_math = sum([row['数学'] for row in grade_table]) / len(grade_table)\n",
    "print('全班数学平均分:', round(avg_math, 2))\n",
    "\n",
    "avg_english = sum([row['英语'] for row in grade_table]) / len(grade_table)\n",
    "print('全班英语平均分:', round(avg_english, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "173b21b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "全班语文平均分: 81.0\n",
      "全班数学平均分: 83.6\n",
      "全班英语平均分: 81.8\n"
     ]
    }
   ],
   "source": [
    "# 函数的定义与调用\n",
    "def average_list(scores):\n",
    "    return sum(scores) / len(scores)\n",
    "\n",
    "average_chinese = average_list([row['语文'] for row in grade_table])\n",
    "print(f'全班语文平均分: {average_chinese:.1f}')\n",
    "\n",
    "average_math = average_list([row['数学'] for row in grade_table])\n",
    "print(f'全班数学平均分: {average_math:.1f}')\n",
    "\n",
    "average_english = average_list([row['英语'] for row in grade_table])\n",
    "print(f'全班英语平均分: {average_english:.1f}')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "113fb922",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三平均分: 84.33333333333333\n",
      "捕获到类型错误(TypeError): unsupported operand type(s) for +: 'int' and 'str'\n"
     ]
    }
   ],
   "source": [
    "# 函数与异常示例\n",
    "def average_three(a, b, c):\n",
    "    return (a + b + c) / 3\n",
    "\n",
    "try:\n",
    "    print('张三平均分:', average_three(78, 85, 90))\n",
    "    # 故意制造一个类型错误\n",
    "    print('错误示例:', average_three(78, '85', 90))\n",
    "except TypeError as e:\n",
    "    print('捕获到类型错误(TypeError):', e)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06666253",
   "metadata": {},
   "source": [
    "### 函数与异常基础\n",
    "- 函数：把重复逻辑封装，提升复用与可读性。\n",
    "- 语法：def 函数名(参数): return 值\n",
    "- 异常 (Exception)：运行出错的事件；用 try/except 捕获并给出友好提示。\n",
    "- 好处：避免 Notebook 因一个可预期错误中断。\n",
    "下面演示自定义平均分函数 + 简单异常处理。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7750be3",
   "metadata": {},
   "source": [
    "### 练习：请你自己再做下面事情 (可在下方新建单元格)\n",
    "1. 增加一位新同学的三科成绩，再重新计算平均分。\n",
    "2. 找出平均分最高的学生姓名与分值。\n",
    "3. 统计英语 >= 80 分的学生数量。\n",
    "\n",
    "> 做完后对照：有没有重复代码？如何提取成函数？(Day 3/4 我们会继续优化)。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af8e1315",
   "metadata": {},
   "source": [
    "## 6. 小结\n",
    "今日学习要点：\n",
    "1. 初步认识数据分析流程的六个阶段。\n",
    "2. 掌握最小 Python 语法集合：变量、条件、循环、容器。\n",
    "3. 用 list + dict 组合模拟“表格”这一核心数据结构的思想。\n",
    "4. 通过平均分 mini case 体验“拆解问题→循环→累积计算”。\n",
    "\n",
    "学习建议：多敲 + 适度复述。遇到错误先读报错行，再查关键词。\n",
    "成长视角：扎实基础、尊重数据真实、循序渐进构建可复现的分析过程。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd0884e1",
   "metadata": {},
   "source": [
    "## 6. 课后作业提示\n",
    "1. 写出**数据分析可应用的 3 个生活/校园场景**（每个 ≥40 字）。例如：食堂满意度、图书借阅偏好、教室使用率。\n",
    "2. 用字典记录你一周学习时间：键=星期，值=小时，例如：{ '周一': 2.5, '周二': 1.0, ... }。\n",
    "3. 计算本周总学习时长，并输出：平均每天学习多少小时。\n",
    "4. 反思：今天最卡的点是什么？写 30~50 字。\n",
    "\n",
    "(提交格式建议：一个新的 Notebook，命名：homework_day1.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7d88bbd",
   "metadata": {},
   "source": [
    "---\n",
    "🎯 下一次课打开本 Notebook 先复现“平均分”代码，再继续。坚持形成自己的 \"可复现场景库\"。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
